/**
 *
*/
package com.bbcare.util.nullCheck;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

import org.apache.commons.lang.StringUtils;

import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.util.FieldNotNull;

/**
 * @Description: TODO
 * @author MLi
 * @date 2018年4月9日
 * @version V1.0
 */
public class StringNull extends ObjectNull {

	@Override
	public void isNullCheck(Method method, Object obj, FieldNotNull fieldNotNull, String nestFieldName) {
		String result = null;
		try {
			result = (String) method.invoke(obj);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
			e1.printStackTrace();
		}
		if(StringUtils.isBlank(result)){
			try{
				throw (new AppException(ErrorCode.DISTREAT_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                    Constants.API_INPUT_PARAMS_DATA_ERROR + "，" + fieldNotNull.desc() + "为空！"));
			} catch (AppException e) {
				throw(e);
			}
		} else {
			int minLength = fieldNotNull.minLength();
			if(result.trim().length() < minLength){
				throw (new AppException(ErrorCode.DISTREAT_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
	                    Constants.API_INPUT_PARAMS_DATA_ERROR + "，" + fieldNotNull.desc() + "长度不合法！"));
			}
			int maxLength = fieldNotNull.maxLength();
			if(result.trim().length() > maxLength){
				throw (new AppException(ErrorCode.DISTREAT_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
	                    Constants.API_INPUT_PARAMS_DATA_ERROR + "，" + fieldNotNull.desc() + "长度不合法！"));
			}
			String[] strValues = fieldNotNull.strValues();
			if(strValues.length > 0 && !Arrays.asList(strValues).contains(result)){
				throw (new AppException(ErrorCode.DISTREAT_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
	                    Constants.API_INPUT_PARAMS_DATA_ERROR + "，" + fieldNotNull.desc() + "取值不合法！"));
			}
		}
	}

}
